package com.yanftch.stopcoding.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SwitchExample {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(SwitchExample::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Swing Switch Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setLayout(new FlowLayout());

        // 创建JToggleButton作为开关
        JToggleButton toggleButton = new JToggleButton("OFF");
        toggleButton.setPreferredSize(new Dimension(100, 50));
        toggleButton.setBackground(Color.RED);

        // 添加动作监听器以处理状态变化
        toggleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (toggleButton.isSelected()) {
                    toggleButton.setText("ON");
                } else {
                    toggleButton.setText("OFF");
                }
            }
        });

        // 将开关添加到面板中
        frame.add(toggleButton);
        frame.setVisible(true);
    }
}
